import { useEffect } from 'react';

const ipcRender = (window as any).listenMain;

export enum TypeKey {
    printSuccess = 'print:success',
    printFail = 'print:fail',
    printFile = 'print:file',
    appName = 'app:name'
}

export default function useIpcListen() {
    return function <T>(type: TypeKey, cb: (_: unknown, value: T) => unknown) {
        const cleanEffect = () => {
            ipcRender.remove(type, cb);
        };

        useEffect(() => {
            ipcRender.listen(type, cb);
            return cleanEffect;
        }, []);

        return cleanEffect;
    };
}
